package com.ws.generation.util;

import com.ws.common.base.entity.Columns;
import com.ws.generation.entity.Config;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {
	

	static String[] fbsArr = { "\\", "$", "(", ")", "*", "+", ".", "[", "]", "?", "^", "{", "}", "|" };  
	static String PACK_NAME="package_name";
	static String TABLE_NAME="table_name";
	static String JAVA_TABLE_NAME="java_table_name";
	static String UPPER_JAVA_TABLE_NAME="upper_java_table_name";
	static String TABLE_FILTER="table_filter";
	static String TABLE_FILED="table_filed";
	static String TABLE_GET_AND_SET="table_get_and_set";
	static String TABLE_ID="table_id";
	
	//下划线转驼峰
	public static String camel(String str) {
		//利用正则删除下划线，把下划线后一位改成大写
		Pattern pattern = Pattern.compile("_(\\w)");
		Matcher matcher = pattern.matcher(str);
		StringBuffer sb = new StringBuffer(str);
		if(matcher.find()) {
			sb = new StringBuffer();
			//将当前匹配子串替换为指定字符串，并且将替换后的子串以及其之前到上次匹配子串之后的字符串段添加到一个StringBuffer对象里。
			//正则之前的字符和被替换的字符
			matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
			//把之后的也添加到StringBuffer对象里
			matcher.appendTail(sb);	
		}else {
			return sb.toString();
		}	
		return camel(sb.toString()).toString();
	}
	

	//替换
	public static String myReplace(String mapperString, Config config) {
		// TODO Auto-generated method stub
		mapperString=mapperString.replaceAll(PACK_NAME, config.getPackageName());
		mapperString=mapperString.replaceAll(UPPER_JAVA_TABLE_NAME, config.getUpperJavaTableName());
		mapperString=mapperString.replaceAll(JAVA_TABLE_NAME, config.getJavaTableName());
		mapperString=mapperString.replaceAll(TABLE_NAME, config.getTableName());
		return mapperString;
	}
	
	//获取字符果过滤
	public static String getStringFilter(List<Columns> columns) {
		// TODO Auto-generated method stub
		String tableFilter="";
		for(Columns column: columns) {
			String columnName=column.getColumnName();
			String javaColumnName=StringUtils.camel(columnName);
			String columnComment=column.getColumnComment();
			String columnType=column.getColumnType();
			String columnKey=column.getColumnKey();
			tableFilter+="		if(searchParam.get(\""+javaColumnName+"\") !=null ";
			if(columnType.contains("varchar")) {
				tableFilter+="	 && !\"\".equals(searchParam.get(\""+javaColumnName+"\").toString() )";
			}
			tableFilter+=") {\n";
			tableFilter+="			sql+=\" and "+columnName+"=#{searchParam."+javaColumnName+"} \";\n";
			tableFilter+="		}\n";
			
    		//System.out.println(column.getColumnName()+"--"+column.getColumnComment()+"--"+column.getColumnType()+"--"+column.getColumnKey());
    	}
		return tableFilter;
	}


	public static String myReplaceTableFilter(String utilString, String tableFilter) {
		// TODO Auto-generated method stub
		utilString=utilString.replaceAll(TABLE_FILTER, tableFilter);
		return utilString;
	}


	public static String getTableFiled(List<Columns> columns) {
		// TODO Auto-generated method stub
		String filedInfo="";
		for(Columns column: columns) {
			String columnName=column.getColumnName();
			String javaColumnName=StringUtils.camel(columnName);
			String columnComment=column.getColumnComment();
			String columnType=column.getColumnType();
			String columnKey=column.getColumnKey();
			String columnString=StringUtils.getColumnString(column.getColumnType());

			filedInfo+="	//"+columnComment+"\n";
			if(columnKey!=null && columnKey.contains("PRI")) {
				filedInfo+="	@TableId\n";
			}
			filedInfo+="	private "+columnString+" "+javaColumnName+";\n";
		}
		return filedInfo;
	}


	private static String getColumnString(String columnType) {
		// TODO Auto-generated method stub
		columnType=columnType.toLowerCase();
		if(columnType.contains("int")) {
			return "Integer";
		}else if( columnType.contains("double") || columnType.contains("float") || columnType.contains("decimal") ) {
			return "Double";
		}else if( columnType.contains("date") || columnType.contains("time")) {
			return "Date";
		}
		return "String";
	}


	public static String getTableGetAndSet(List<Columns> columns) {
		// TODO Auto-generated method stub
		String getAndSet="";
		for(Columns column: columns) {
			String columnName=column.getColumnName();
			String javaColumnName=StringUtils.camel(columnName);
			String columnComment=column.getColumnComment();
			String columnType=column.getColumnType();
			String columnKey=column.getColumnKey();
			String columnString=StringUtils.getColumnString(column.getColumnType());
			String upperJavaColumnName=StringUtils.upperCamel(javaColumnName);
			
			getAndSet+="	public "+columnString+" get"+upperJavaColumnName+"() {\n";
			getAndSet+="		return "+javaColumnName+";\n";
			getAndSet+="	}\n";
			
			getAndSet+="	public void set"+upperJavaColumnName+"("+columnString+" "+javaColumnName+") {\n";
			getAndSet+="		this."+javaColumnName+" = "+javaColumnName+";\n";
			getAndSet+="	}\n";
			
		}
		return getAndSet;
	}


	private static String upperCamel(String columnName) {
		// TODO Auto-generated method stub
		
		return columnName.substring(0, 1).toUpperCase()+columnName.substring(1);
	}


	public static String myReplaceEntityString(String entityString, String tableFiled, String tableGetAndSet, String tableID) {
		// TODO Auto-generated method stub
		entityString=entityString.replaceAll(TABLE_FILED, tableFiled);
		entityString=entityString.replaceAll(TABLE_GET_AND_SET, tableGetAndSet);
		entityString=entityString.replaceAll(TABLE_ID, tableID);
		return entityString;
	}


	public static String getTableId(List<Columns> columns) {
		
		String tableId="";
		for(Columns column: columns) {
			String columnName=column.getColumnName();
			String javaColumnName=StringUtils.camel(columnName);
			String columnKey=column.getColumnKey();
			if(columnKey!=null && columnKey.contains("PRI")) {
				tableId=javaColumnName;
			}
		}
		return tableId;
	}
}
